<template>
  <div class="overlay" v-if="showDialog">
    <div class="wx-pay">
      <div class="wx-pay__header">
        <div>微信扫码支付</div>

        <img :src="closeImg" alt="" @click="handleClick" />
      </div>
      <div class="wx-pay__money">
        支付金额 <span class="pl-[8px]">￥</span>
        <span class="font-gilroy-bold money">{{ money }}</span>
      </div>
      <div class="wx-pay__code">
        <div class="code-box">
          <img :src="payCode" alt="" />
        </div>
        <img :src="guide" alt="" />
      </div>
      <div class="wx-pay__tips">请使用 <span>微信</span>「扫一扫」二维码完成支付</div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { toDataURL } from 'qrcode'
import guide from '@/assets/images/cashier/guide.png'
import closeImg from '@/assets/images/closeIcon.png'

import { getOrderStatus } from '../../../../shared/apis'

const props = defineProps<{
  show: boolean
  money: string | number
  url: string
}>()

const emit = defineEmits<{
  (e: 'update:show', val: boolean): void
}>()
// const { orderApi } = useApi()
const showDialog = ref(false)
const route = useRoute()
const { orderSn, redirectUri, orderSns } = route.query as Record<string, string>

watch(
  () => props.show,
  (val) => {
    showDialog.value = val
  }
)

const timer = ref()
const getPayStatus = async () => {
  // const { error, data } = await orderApi.getOrderPayStatus(orderParams)
  // if (!error.value) {
  //   const { code } = data.value.data
  //   if (code === PayCode.Paid) {
  //     // 需要返回当前租户的域名下单支付成功  将redirectUri 解码
  //     if (redirectUri) {
  //       window.location.href = decodeURIComponent(redirectUri as string)
  //     } else {
  //       // Message('redirect_url没有配置')
  //     }
  //     clearInterval(timer.value)
  //     showDialog.value = false
  //     emit('update:show', false)
  //   }
  // }
  const params = {
    orderSn: orderSn
    // orderSns: orderSns ? JSON.parse(orderSns) : undefined
  }

  const { data } = await getOrderStatus(params)
  console.log(data, 'data')
}

watch(
  () => props.url,
  (val) => {
    generateQR(val)

    // 使用websocket监听支付状态
    timer.value = setInterval(() => {
      getPayStatus()
    }, 2000)
  }
)

const handleClick = () => {
  clearInterval(timer.value)
  showDialog.value = false

  emit('update:show', false)
}

const payCode = ref('')
const generateQR = async (text: string) => {
  try {
    payCode.value = await toDataURL(text, {
      errorCorrectionLevel: 'H',
      width: 148,
      margin: 0
    })
  } catch (err) {
    console.error(err)
  }
}
</script>

<style scoped lang="scss">
.overlay {
  display: flex;
  height: 100%;
  left: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  width: 100%;
  align-items: center;
  justify-content: center;
  z-index: 1111;
  background-color: rgba(0, 0, 0, 0.3);
}
.wx-pay {
  padding: 16px;
  width: 640px;
  height: 418px;
  background: #ffffff;
  border-radius: 12px;
  &__header {
    display: flex;
    justify-content: space-between;
    height: 48px;
    padding-left: 6px;
    font-size: 20px;
    font-weight: 500;
    color: #111;
    div {
      line-height: 48px;
    }
    img {
      width: 32px;
      height: 32px;
      cursor: pointer;
    }
  }
  &__money {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 24px;
    text-align: center;
    font-size: 14px;
    font-family:
      PingFangSC-Regular,
      PingFang SC;
    font-weight: 400;
    color: #111111;

    span {
      @apply font-semibold text-red;
      font-size: 20px;
    }
    span .money {
      font-size: 24px;
      font-weight: 600;
    }
  }
  &__code {
    margin-top: 24px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    .code-box {
      padding: 16px;
      width: 180px;
      height: 180px;
      background: rgba(62, 223, 146, 0.05);
      border-radius: 12px;
      border: 2px solid #3edf92;
      overflow: hidden;
    }
    & > img {
      margin-left: 5px;
      margin-right: 58px;
      width: 151px;
      height: 136px;
    }
  }

  &__tips {
    margin-top: 24px;
    font-size: 14px;
    font-family:
      PingFangSC-Regular,
      PingFang SC;
    font-weight: 400;
    text-align: center;
    color: #111111;
    span {
      color: var(--kkc-red);
    }
  }
}
</style>
